package org.example;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.json.JsonObject;
import com.couchbase.client.java.kv.GetResult;
import com.couchbase.client.java.kv.MutationResult;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        Cluster cluster = Cluster.connect("192.168.56.12", "root", "123456");
        Bucket bucket = cluster.bucket("spring"); // bucket
        Collection collection = bucket.defaultCollection();

        for(int i=0;i<5000;i++) {
            long j=i;
            MutationResult upsertResult = collection.upsert(
                    "comment-"+j,
                    JsonObject.create()
                            .put("name", "michaels-"+j)
                            .put("count", j)
                            .put("helpfulCount", j)
                            .put("unhelpfulCount", j)
                            .put("sub", JsonObject.create()
                                    .put("hello", j))
            );
            if(i%1000==0) {
                System.out.println(i);
            }
        }

        for(int i=0;i<5000;i++) {
            String id="comment-" + i;
            GetResult getResult = collection.get(id);
            Integer count = getResult.contentAsObject().getInt("count");
            if(count!=i) {
                throw new RuntimeException(String.format("Expected: %d, Found: %d!", i, count));
            }
            if(i%1000==0) {
                System.out.println(i);
            }
        }
    }
}
